package bairui.international.console.web.controller.user.royalty;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.QTB_BranchCompanyStaffRoyaltyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffRoyaltyRecord;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.model.type.Type_ProductOrderPay;
import bairui.international.service.Service_BranchCompanyStaffRoyaltyRecord;

/**
 *
 * 列表
 *
 * @generated
 * 
 * @see STest_C_User_Royalty_Page_Page 测试
 *
 */
@Controller
public class C_User_Royalty_Page_Page
 extends SC_User_Royalty_Page_Page 
{
	
	@Autowired private Service_BranchCompanyStaffRoyaltyRecord service_BranchCompanyStaffRoyaltyRecord;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request, 
		@PathVariable(name=Page.PathVariableName.UserId) long userId
	) throws Exception {
		org.springframework.data.domain.Page<TB_BranchCompanyStaffRoyaltyRecord> page = service_BranchCompanyStaffRoyaltyRecord.page(
				it->filter(it, userId, request), 
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		return Page.Output.output(
				page
					.map(it->new Page.Output.Model(it){
							private TB_ProductOrder productOrder;
							
							@Override protected void assign_before() {
								productOrder = it.getProductOrder();
							}
							@Override public Double assignPrice() {
								return productOrder.getPrice();
							}
							@Override public String assignNumber() {
								return productOrder.getNumber();
							}
							@Override public java.util.Date assignSubmitDate() {
								return productOrder.getSubmitDate();
							}
							@Override public String assignUserName() {
								if(null == productOrder.getUserInfo()){
									return null;
								}
								return productOrder.getUserInfo().getRealName();
							}
							@Override public String assignAreaRecord() {
								return productOrder.getAreaRecord();
							}
							@Override public State_ProductOrder assignState() {
								return productOrder.getState();
							}
							@Override public Type_ProductOrderPay assignPayWay() {
								return productOrder.getPayWay();
							}
						}
					)
			);
	}

	static BooleanBuilder filter(QTB_BranchCompanyStaffRoyaltyRecord it, long userId, HttpServletRequest request){
		return Page.Filter.query(it, request, new Page.Filter.FilterExtends() {
			@Override public Predicate gainUserName(String userName) {
				return it.productOrder.userInfo.realName.like("%" + userName + "%");
			}
			@Override public Predicate gainNumber(String number) {
				return it.productOrder.number.like("%" + number + "%");
			}
			@Override public Predicate gainState(State_ProductOrder state) {
				return it.productOrder.state.eq(state);
			}
			@Override public Predicate gainPayWay(Type_ProductOrderPay payWay) {
				return it.productOrder.payWay.eq(payWay);
			}
			@Override public Predicate gainSubmitDate_start(Date submitDate) {
				return it.productOrder.submitDate.gt(submitDate);
			}
			@Override public Predicate gainSubmitDate_end(Date submitDate) {
				return it.productOrder.submitDate.lt(submitDate);
			}
		}).and(it.branchCompanyStaff.user.id.eq(userId));
	}
	
	static Sort sort(QTB_BranchCompanyStaffRoyaltyRecord it, HttpServletRequest request){
		return Page.Output.sort(it, request, new Page.Output.OrderExtends() {
			@Override public Sort gainUserName(Direction direction) {
				return sort(direction, it.productOrder.userInfo.realName);
			}
			@Override public Sort gainSubmitDate(Direction direction) {
				return sort(direction, it.productOrder.submitDate);
			}
			@Override public Sort gainState(Direction direction) {
				return sort(direction, it.productOrder.state);
			}
			@Override public Sort gainAreaRecord(Direction direction) {
				return sort(direction, it.productOrder.areaRecord);
			}
			@Override public Sort gainPayWay(Direction direction) {
				return sort(direction, it.productOrder.payWay);
			}
			@Override public Sort gainNumber(Direction direction) {
				return sort(direction, it.productOrder.number);
			}
			@Override public Sort gainPrice(Direction direction) {
				return sort(direction, it.productOrder.price);
			}
		});
	}
	
}
